package com.anyun.collect.customview

import android.content.Context
import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.*
import com.anyun.collect.R


/**collect
 * Created by lzw on 2020/1/3. 16:18:27
 * 邮箱：632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
class SimpleDividerItemDecoration : ItemDecoration {
    private var mDivider //分割线Drawable
            : Drawable? = null
    private var mDividerHeight //分割线高度
            : Int
    private var inset //分割线缩进值
            = 0

    /**
     * 使用line_divider中定义好的颜色
     *
     * @param context
     * @param dividerHeight 分割线高度
     */
    constructor(context: Context?, dividerHeight: Int) {
        mDivider = context?.let { ContextCompat.getDrawable(it, R.drawable.line_divider) }
        mDividerHeight = dividerHeight
    }

    constructor(context: Context?, inset: Int, dividerHeight: Int) {
        this.inset = inset
        mDivider = context?.let { ContextCompat.getDrawable(it, R.drawable.line_divider) }
        mDividerHeight = dividerHeight
    }

    /**
     * @param context
     * @param divider       分割线Drawable
     * @param dividerHeight 分割线高度
     */
    constructor(context: Context?, divider: Drawable?, dividerHeight: Int) {
        mDivider = divider ?: context?.let { ContextCompat.getDrawable(it, R.drawable.line_divider) }
        mDividerHeight = dividerHeight
    }

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: State) {
        super.getItemOffsets(outRect, view, parent!!, state!!)
        outRect.set(0, 0, 0, mDividerHeight)
    }

    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: State) {
        val left = parent.paddingLeft
        val right = parent.width - parent.paddingRight
        val childCount = parent.childCount
        //最后一个item不画分割线
        for (i in 0 until childCount - 1) {
            val child: View = parent.getChildAt(i)
            val params =
                child.getLayoutParams() as LayoutParams
            val top: Int = child.getBottom() + params.bottomMargin
            //            int bottom = top + mDividerHeight;
            val bottom = top + mDivider!!.intrinsicHeight
            if (inset > 0) {
                mDivider!!.setBounds(left + inset, top, right - inset, bottom)
            } else {
                mDivider!!.setBounds(left, top, right, bottom)
            }
            mDivider!!.draw(c)
        }
    }

}